﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace ttlib
{
    static class HashTool
    {
        [ThreadStatic]
        static SHA256 sha256;
        public static byte[] Sha256(byte[] data)
        {
            if (sha256 == null)
                sha256 = SHA256.Create();
            return sha256.ComputeHash(data);
        }
        [ThreadStatic]
        static StringBuilder hexsb ;
        public static string Hex2Str(byte[] data)
        {
            if(hexsb==null)
                hexsb=new StringBuilder();
            hexsb.Clear();
            for(var i= 0;i < data.Length;i++)
            {
                hexsb.AppendFormat("{0:x02}", data[i]);
            }
            return hexsb.ToString();
        }
        public static byte[] Str2Hex(string hexstr)
        {
            byte[] data=new byte[hexstr.Length/2];
            for(var i=0;i< data.Length;i++)
            {
                data[i]=byte.Parse(hexstr.Substring(i*2,2),System.Globalization.NumberStyles.HexNumber);
            }
            return data;
        }
    }
}
